﻿#define N 10000
#define a 10
#define b 100
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <mpi.h>
int main(int argc, char** argv)
{
	int myid, numprocs;
	int i;
	double local = 0.0, dx = (double)(b - a) / N;
	double inte, x, Sum = 0.0;
	MPI_Status status;
	MPI_Init(&argc, &argv);
	MPI_Comm_rank(MPI_COMM_WORLD, &myid);
	MPI_Comm_size(MPI_COMM_WORLD, &numprocs);
	--numprocs;
	if (myid == 0)
	{	
		for (int source = 1; source <= numprocs; ++source)
		{
			MPI_Recv(&inte, 1, MPI_DOUBLE, source, 0, MPI_COMM_WORLD, &status);
			Sum = Sum + inte;
		}
		printf("The integal of x*x in region [%d,%d] =%16.15f\n", a, b, Sum);
	}	
	else
	{
	for (i = myid; i < N; i = i + numprocs)
	{		
		x = a + i * dx + dx / 2;
		local += x* x * x * dx;
	}
	MPI_Send(&local, 1, MPI_DOUBLE, 0, 0, MPI_COMM_WORLD);
	}	
	MPI_Finalize();
}
